package cn.ccit.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ccit.base.BaseConstants;
import cn.ccit.entites.SystemRole;
import cn.ccit.entites.User;
import cn.ccit.entites.UserAuthority;
import cn.ccit.iservers.ISystemRoleServers;
import cn.ccit.iservers.IUserServers;
import cn.ccit.model.BaseModel;
import cn.ccit.servers.SystemRoleServers;
import cn.ccit.servers.UserAuthorityServers;
import cn.ccit.servers.UserServers;
import cn.ccit.util.GsonUtil;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends BaseAction implements ModelDriven<User> {
	/**  
	 * @Fields serialVersionUID : v1.0  
	 */ 
	private static final long serialVersionUID = 1L;
	private IUserServers iUserServers=new UserServers();
	private User user=new User();
	private ISystemRoleServers simp=new SystemRoleServers();
	private UserAuthorityServers uaimp=new UserAuthorityServers();
	private List<User> l;
	
	public String login() {
		BaseModel<User> userModel =new BaseModel<User>();
		User u=iUserServers.loginUser(user);
		if(u!=null){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			SystemRole sr=simp.searchSystemRoleByID(u.getRoleId());
			List<UserAuthority> ualist=uaimp.getUserAuthoritiesFromIdList(sr.getAuthoritiesIdList());
			Map<String, Object> temp=new HashMap<String, Object>();
			temp.put("user", u);
			temp.put("role", sr);
			temp.put("ualist", ualist);
			for (UserAuthority userAuthority : ualist) {
				System.out.println(userAuthority.getName());
			}
			sendJsonDataMap(temp, new BaseModel<Map<String,Object>>());
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	
	public String regUser() {//用户注册
		BaseModel<User> userModel =new BaseModel<User>();//新建用户传输实体类模型
		if(iUserServers.regUser(user)){//判断用户注册是否成功
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);//返回成功状态码200
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);//状态信息 OK
			userModel.setData(user);
			
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);//错误状态码 101
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);//错误信息 参数错误
		}
		GsonUtil.sendJson(userModel);//将数据转换为json发送
		return Action.SUCCESS;
	}
	
	public String changeUserInfo() {
		BaseModel<User> userModel =new BaseModel<User>();
		if(iUserServers.changeUserInfo(user)){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	public String getSomeUsers() {
		BaseModel<List<User>> userModel =new BaseModel<List<User>>();
		l = iUserServers.getSomeUsers(startId, limit);
		if(l!=null&&l.size()>0){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			userModel.setData(l);
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	public String searchUserInfoByName() {
		BaseModel<List<User>> userModel =new BaseModel<List<User>>();
		l=iUserServers.searchUserInfoByName(user.getName());
		if(l!=null&&l.size()>0){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			userModel.setData(l);
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	public String searchUserInfoByID() {
		BaseModel<List<User>> userModel =new BaseModel<List<User>>();
		l=new ArrayList<User>();
		l.add(iUserServers.searchUserInfoByID(user.getId()));
		if(l!=null&&l.size()>0){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			userModel.setData(l);
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	
	public String removeUser() {
		BaseModel<User> userModel =new BaseModel<User>();
		if(iUserServers.removeUser(user)){
			userModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			userModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
		}else{
			userModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			userModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(userModel);
		return Action.SUCCESS;
	}
	
	@Override
	public User getModel() {
		return user;
	}

	
}
